﻿using System;
using System.Collections.Generic;
using System.Configuration;

using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web.UI.WebControls;

namespace Lib.Common
{
    public class ComomFunctions
    {
        
        #region Convert bit sang số nguyên
        public static int BinToInt(string binaryNumber)
        {
            int multiplier = 1;
            int i;
            int converted = 0;
            for (i = 0; i <= binaryNumber.Length - 1; i++)
            {
                int t = System.Convert.ToInt16(binaryNumber[i].ToString());
                converted = converted + (t * multiplier);
                multiplier = multiplier * 2;
            }
            return converted;
        }
        #endregion

        /// <summary>
        /// Hàm tính số mũ
        /// </summary>
        /// <param name="mu">Số mũ</param>
        /// <returns></returns>
        public static int HamMu(int mu)
        {
            int s = 1;
            for (int i = 0; i < mu; i++)
            {
                s = s * 2;
            }
            return s;
        }

        public static bool Check(int CheckStatus, int CurrentStatus)
        {
            int _Temp = CurrentStatus & CheckStatus;
            if (_Temp != CheckStatus)
                return false;
            return true;
        }
       

        public static int ReturnStatusCheck(CheckBox[] CheckBox)// Trả về 1 dãy bit
        {
            string ValueCheck = "";
            for (int i = 0; i < CheckBox.Length; i++)
            {
                if (CheckBox[i].Checked == true)
                    ValueCheck = ValueCheck + "1";
                else
                    ValueCheck = ValueCheck + "0";
            }
            return Convert.ToInt32(BinToInt(ValueCheck));
        }

        public static bool SelectChecked(int Role, int CheckRole, CheckBox[] CheckBox)//Trả về True khi CheckBox được Checked
        {
            bool KQ = false;
            for (int i = 0; i < CheckBox.Length; i++)
            {
                if (Check(Role, CheckRole) == true)
                    return true;
                else
                    return false;
            }
            return KQ;
        }

        public static void SendMail(string subject, string body, string to)
        {
            //MailMessage mail = new MailMessage();
            //mail.From = new MailAddress(ConfigurationSettings.AppSettings["emailsend"].ToString());
            //mail.To.Add(to);
            //mail.Subject = subject;
            //mail.Body = body;
            //mail.IsBodyHtml = true;
            //SmtpClient smtp = new SmtpClient(ConfigurationSettings.AppSettings["smtp"].ToString());
            //NetworkCredential Credentials = new NetworkCredential(ConfigurationSettings.AppSettings["emailsend"].ToString(), ConfigurationSettings.AppSettings["pass"].ToString());
            //smtp.Credentials = Credentials;
            //smtp.Send(mail);

            string from = ConfigurationSettings.AppSettings["emailsend"].ToString(); //Replace this with your own correct Gmail Address                    
            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
            mail.To.Add(to);
            mail.From = new System.Net.Mail.MailAddress(from, "ngoaihanganh.vn", System.Text.Encoding.UTF8);
            mail.Subject = subject;
            mail.SubjectEncoding = System.Text.Encoding.UTF8;
            mail.Body = body;
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.IsBodyHtml = true;
            //mail.Priority = System.Net.Mail.MailPriority.High;

            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
            //Add the Creddentials- use your own email id and password

            client.Credentials = new System.Net.NetworkCredential(from, "hodangnhatthi");

            client.Port = 587; // Gmail works on this port
            client.Host = ConfigurationSettings.AppSettings["smtp"].ToString();
            client.EnableSsl = true; //Gmail works on Server Secured Layer
            try
            {
                client.Send(mail);
            }
            catch (Exception ex)
            {
                Exception ex2 = ex;
                string errorMessage = string.Empty;
                while (ex2 != null)
                {
                    errorMessage += ex2.ToString();
                    ex2 = ex2.InnerException;
                }
                //HttpContext.Current.Response.Write(errorMessage);
            } // end try 


        }
    }
}
